package com.lgr.store.interceptor;


import com.lgr.store.utils.ContextUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.lgr.store.service.CharactersService;
import com.lgr.store.utils.AppointStateEnum;
import com.lgr.store.utils.LogJournal;
import com.lgr.store.vo.EmpVo;
import com.lgr.store.vo.ModuleVo;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
//权限控制的拦截器
public class AdminInterceptor implements HandlerInterceptor {

    @Resource
    private HttpSession session;
    @Resource
    private CharactersService charactersService;
    @Resource
    private LogJournal logJournal;
    //方法执行之前进行拦截（做权限控制）
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        String path = request.getContextPath();
        String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
        //获取用户登录session
        EmpVo emp = ContextUtils.getUserInfo();
        if(emp==null){//表示用户未登录，跳转到登录页面
            response.sendRedirect(basePath + "admin/logins");
            return false;
        }
        //读取系统权限,保存到application对象中
        List<ModuleVo> moduleList = (List<ModuleVo>) session.getServletContext().getAttribute("moduleList");
        if (moduleList == null) {
            moduleList = charactersService.selectModule();
            session.getServletContext().setAttribute("moduleList", moduleList);
        }
        //获取用户权限
        boolean index = true;
        List<ModuleVo> empModule = emp.getModuleEmpList();//获取用户权限
        System.out.println("request.getServletPath()="+request.getServletPath());
        for (ModuleVo module : moduleList) {
            //判断是否在权限中
            if (request.getServletPath().equals(module.getController())) {
                index = false;
                for (ModuleVo module2 : empModule) {
                    //判断用户权限中是否有对应的权限
                    if(module.getModuleId()==module2.getModuleId()){
                        return true;
                    }
                }
            }
        }
        if(!index){
            response.sendRedirect(basePath + "admin/error");
        }
        return index;
    }
    //方法执行成功后进行拦截(做日志控制)
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView modelAndView) throws Exception {
        Map<Integer,Object> logSession= (Map<Integer, Object>) session.getAttribute(AppointStateEnum.LOG.getStateInfo());
        if(logSession!=null){
            logJournal.saveUserLog(logSession);
        }
        session.removeAttribute(AppointStateEnum.LOG.getStateInfo());

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object o, Exception e) throws Exception {

    }
}
